package com.futures.questionbank.ui.home;

import android.content.Intent;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.futures.questionbank.Constants.MessageEvent;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.adapter.BaseViewHolder;
import com.futures.questionbank.adapter.ExamAdpter;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.Constants;
import com.futures.questionbank.db.table.exam;
import com.futures.questionbank.main.ExamActivity2;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/6/17 0017 9:16
 * version: 1.0
 */
public class LianXiActivity extends BaseActivity {
    @BindView(R.id.rv)
    RecyclerView rv;

    private List<exam> users;
    private List<exam> users2 = new ArrayList<>();
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.zhengti_activity);
        setTitle("模拟卷");

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(Constants.FAMILY)) {
                int value = messageEvent.getMessageValue();
                users = SQLite.select().
                        from(exam.class).
                        queryList();
                users2.clear();
                for (exam user : users) {
                    if (user.getId() > 49){
                        users2.add(user);
                    }
                }

                //   String a = df.format((float)i/b);
                //  String a = (i/b)*100 + "";
                ExamAdpter chapterAdpter = new ExamAdpter(LianXiActivity.this, users2, new LianXiActivity.TypeItemListener());
                rv.setAdapter(chapterAdpter);
            }
        }
    }


    @Override
    protected void loadData() {
        UItils.showDialog(this);
        users = SQLite.select().
                from(exam.class).
                queryList();
        users2.clear();
        for (exam user : users) {
            if (user.getId() > 49){
                users2.add(user);
            }
        }

        rv.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        ExamAdpter chapterAdpter = new ExamAdpter(LianXiActivity.this, users2, new LianXiActivity.TypeItemListener());
        rv.setAdapter(chapterAdpter);

    }

    class TypeItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            Intent intent = new Intent();
            intent.putExtra("index", users2.get(position).getId());
            intent.setClass(LianXiActivity.this, ExamActivity2.class);
            startActivity(intent);
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }
}
